package main

import . "local/algorithm/util"

func lowestComonAncestorOfbst(root, p, q *TreeNode) *TreeNode {
	if root == nil {
		return nil
	}

	if p == root || q == root {
		return root
	}

	left := lowestComonAncestorOfbst(root.Left, p, q)
	right := lowestComonAncestorOfbst(root.Right, p, q)
	if left != nil && right != nil {
		return root
	} else if left != nil {
		return left
	} else {
		return right
	}
}
